var rewire = require('rewire')
var testcases = rewire('../../controller/testCasesController.js')
var expect = require('chai').expect

var testcaseData = [{
  'msgOut': {
    'dbVerifier': 'select DECODE(CHKPT_STATE, \'Pending\', \'true\', \'false\') as result from D_CHECKPOINT where CHKPT_TYPE = \'OB_BKG_CONF\' and DOMAIN_OBJECT_KEY = \'@shipmentNumber@\';',
    'mongoDBVerifier': '[{"table":"processInstances","criteria":{"domObjKey":"@shipmentNumber@","chkptStatus":"Pending","chkptType":"OB_BKG_CONF"}},{"table":"processInstances","criteria":{"chkptType":"OB_SI_RECV","chkptStatus":"Pending","domObjKey":"@shipmentNumber@"}}]',
    'messageVerifier': '<?xml version="1.0" encoding="UTF-8"?>\n\n<ns0:PCM_ProcessData xmlns:ns0="http://com.oocl.schema.common">\n    <ns0:CheckpointEvent xmlns:ns0="http://com.oocl.schema.common">\n        <ns0:Checkpoint_ID>\n            <ns0:DomainObjectRef>\n                <ns0:Namespace>com.oocl.ir4.pcm</ns0:Namespace>\n                <ns0:DomainObjectType>Shipment</ns0:DomainObjectType>\n                <ns0:DomainObjectKey>@shipmentNumber@</ns0:DomainObjectKey>\n            </ns0:DomainObjectRef>\n            <ns0:CheckpointType>OB_BKG_CONF</ns0:CheckpointType>\n        </ns0:Checkpoint_ID>\n        <ns0:Instance_ID>317957d9-9f20-4d9f-a59a-46a2143eb110</ns0:Instance_ID>\n        <ns0:CheckpointState>Pending</ns0:CheckpointState>\n    </ns0:CheckpointEvent>\n</ns0:PCM_ProcessData>'
  },
  'verifiers': [
    'DB Verifier',
    'MongoDB Verifier'
  ],
  'urls': [
    {
      'url': '/wls_dom_shp/rest/shipment/lw/checkPointDoc/xml/@shipmentNumber@'
    },
    {
      'url': '/wls_dom_shp/rest/shipment/lw/cacheLWShp/xml/@shipmentNumber@?V=12'
    },
    {
      'url': '/wls_dom_shp/rest/shipment/lw/sf/xml/@shipmentNumber@?V=5'
    },
    {
      'url': '/wls_dom_shp/rest/shipment/lw/xml/@shipmentNumber@?V=16'
    },
    {
      'url': '/wls_dom_shp/rest/dndData/@CMSReference@?locationRole=Outbound&dndType=all'
    },
    {
      'url': '/wls_dom_shp/rest/shipment/lw/cmspec/xml/@CMSReference@?V=15'
    }
  ],
  'tags': [],
  '_id': '5aa88ae325b4ff6361064ba9',
  'testCaseId': '161b8f18-eb6a-4fbf-905b-d5e366dc03ca',
  'testCaseName': 'Pending booking',
  'create_date': '14 Mar 2018 10:37',
  'last_update_date': '17 Apr 2018 14:35',
  '__v': 0,
  'bizKey': [{'bizKeyType': 'shipmentNumber', 'bizKeyValue': '123456'}, {'bizKeyType': 'CMSReference', 'bizKeyValue': 'cms123'}],
  'msgIn': '$Header:\r\nJMSMessageID=ID:IRIS4-EMS-SERVER.24BE5A2913C21:29D45150F\r\nJMSTimestamp=Wed Mar 14 10:13:21 645 CST 2018\r\nJMSDestination=Queue[IR4.SHP.SHP.UPDATE]\r\nJMSDeliveryMode=PERSISTENT\r\nJMSPriority=4\r\n$Properties:\r\nesmHashString=String:{"FCL_SHP_CargoUpdate":"1a5656084b4d6f9e785488b594d857016829a82d","SHP_CgoNature_Upd":"6e6ba57f8d1da018eda92d0864b03277a6b25229","SHPUpdate_Dnd":"73842513574c0e6a2f99eb1331f585f25eb379ed","IBDV_SHP_InfoUpdate":"512748c3ae8097fccb6db0dcec6ceaf628c80dea","SHP_AgmtInfo_Upd":"73842513574c0e6a2f99eb1331f585f25eb379ed","VSCM_SHP_InfoUpdate":"dd1a6e540e26a1e876ea1b16f9ed7c16bdc030bc","SHP_Info_Update_CMTX":"81388da12dfc87898218dec498a729b148420165","SHP_CMSUpdate":"dec0af08c37b10b89413de4848ae0586b7494433","SHP_InfoUpdate":"6593ccf7da474ab595091bbec6464490699ef47e","ShipmentPartyUpdate_OBLeg":"86c3c31f08b9ed65bd1cd6366ad4ea1617dc2501","SHP_CMSUpdate2":"a19c14ba37c4dbdb24a16fce325213dea155e167","SHP_InfoUpdateForDSH":"2f29545f7f76c433d3edd11e8eccbe8ce1e40d9a","VID_SHPUpdate":"160c7246f40cb0ab1c0acad087343fb8db316391"}\r\nMF_SenderDestName=String:IR4.EVG.SHP.UPDATE\r\nIR2NP_SenderCFName=String:MessageFrameworkXACF\r\nMF_AppNamespace=String:http://com.oocl.ir4/shp\r\nIR2NP_AppNamespace=String:http://com.oocl.ir4/shp\r\nIR2NP_MessageVersion=String:1.0\r\nMF_RootElement=String:SHIPMENT_EVT\r\nIR2NP_SenderDestName=String:IR4.EVG.SHP.UPDATE\r\nJMS_TIBCO_PRESERVE_UNDELIVERED=Boolean:true\r\nXCID=String:C7E8AF62-3130-DB25-5375B7F32A6B569F\r\nMF_MessageVersion=String:1.0\r\nJMSXDeliveryCount=Integer:1\r\nDomainId=String:SHP\r\nIR2NP_MessageTimeToLive=Long:0\r\nIR2NP_MessageType=String:SHIPMENT_EVT\r\nMF_SenderCFName=String:MessageFrameworkXACF\r\nProcessId=String:4e947a85-0c72-47c8-9908-380b8bf87ec7\r\nMF_MessageTimeToLive=Long:0\r\nCARRIER_ID=Byte:1\r\nMessageSentTime=String:20180314021321.000\r\n$TextBody:\r\n<?xml version="1.0" encoding="UTF-8" standalone="yes"?><Shipment_EVT xmlns="http://com.oocl.ir4.schema.shp" xmlns:ns2="http://com.oocl.ir4.shp" xmlns:ns4="http://com.oocl.ir4.schema.acm" xmlns:ns3="http://com.oocl.ir4.schema.its"><ShipmentNumber>@shipmentNumber@</ShipmentNumber></Shipment_EVT>\r\n',
  'inQueue': 'IR4.SHP.SHP.UPDATE',
  'outQueue': 'IR4.SHPP.WF.REQ'
}]

var resultData = {
  'msgOut': {
    'dbVerifier': 'select DECODE(CHKPT_STATE, \'Pending\', \'true\', \'false\') as result from D_CHECKPOINT where CHKPT_TYPE = \'OB_BKG_CONF\' and DOMAIN_OBJECT_KEY = \'123456\';',
    'mongoDBVerifier': '[{"table":"processInstances","criteria":{"domObjKey":"123456","chkptStatus":"Pending","chkptType":"OB_BKG_CONF"}},{"table":"processInstances","criteria":{"chkptType":"OB_SI_RECV","chkptStatus":"Pending","domObjKey":"123456"}}]',
    'messageVerifier': '<?xml version="1.0" encoding="UTF-8"?>\n\n<ns0:PCM_ProcessData xmlns:ns0="http://com.oocl.schema.common">\n    <ns0:CheckpointEvent xmlns:ns0="http://com.oocl.schema.common">\n        <ns0:Checkpoint_ID>\n            <ns0:DomainObjectRef>\n                <ns0:Namespace>com.oocl.ir4.pcm</ns0:Namespace>\n                <ns0:DomainObjectType>Shipment</ns0:DomainObjectType>\n                <ns0:DomainObjectKey>123456</ns0:DomainObjectKey>\n            </ns0:DomainObjectRef>\n            <ns0:CheckpointType>OB_BKG_CONF</ns0:CheckpointType>\n        </ns0:Checkpoint_ID>\n        <ns0:Instance_ID>317957d9-9f20-4d9f-a59a-46a2143eb110</ns0:Instance_ID>\n        <ns0:CheckpointState>Pending</ns0:CheckpointState>\n    </ns0:CheckpointEvent>\n</ns0:PCM_ProcessData>'
  },
  'verifiers': [
    'DB Verifier',
    'MongoDB Verifier'
  ],
  'urls': [
    {
      'url': '/wls_dom_shp/rest/shipment/lw/checkPointDoc/xml/123456'
    },
    {
      'url': '/wls_dom_shp/rest/shipment/lw/cacheLWShp/xml/123456?V=12'
    },
    {
      'url': '/wls_dom_shp/rest/shipment/lw/sf/xml/123456?V=5'
    },
    {
      'url': '/wls_dom_shp/rest/shipment/lw/xml/123456?V=16'
    },
    {
      'url': '/wls_dom_shp/rest/dndData/cms123?locationRole=Outbound&dndType=all'
    },
    {
      'url': '/wls_dom_shp/rest/shipment/lw/cmspec/xml/cms123?V=15'
    }
  ],
  'tags': [],
  '_id': '5aa88ae325b4ff6361064ba9',
  'testCaseId': '161b8f18-eb6a-4fbf-905b-d5e366dc03ca',
  'testCaseName': 'Pending booking',
  'create_date': '14 Mar 2018 10:37',
  'last_update_date': '17 Apr 2018 14:35',
  '__v': 0,
  'bizKey': [{'bizKeyType': 'shipmentNumber', 'bizKeyValue': '123456'}, {'bizKeyType': 'CMSReference', 'bizKeyValue': 'cms123'}],
  'msgIn': '$Header:\r\nJMSMessageID=ID:IRIS4-EMS-SERVER.24BE5A2913C21:29D45150F\r\nJMSTimestamp=Wed Mar 14 10:13:21 645 CST 2018\r\nJMSDestination=Queue[IR4.SHP.SHP.UPDATE]\r\nJMSDeliveryMode=PERSISTENT\r\nJMSPriority=4\r\n$Properties:\r\nesmHashString=String:{"FCL_SHP_CargoUpdate":"1a5656084b4d6f9e785488b594d857016829a82d","SHP_CgoNature_Upd":"6e6ba57f8d1da018eda92d0864b03277a6b25229","SHPUpdate_Dnd":"73842513574c0e6a2f99eb1331f585f25eb379ed","IBDV_SHP_InfoUpdate":"512748c3ae8097fccb6db0dcec6ceaf628c80dea","SHP_AgmtInfo_Upd":"73842513574c0e6a2f99eb1331f585f25eb379ed","VSCM_SHP_InfoUpdate":"dd1a6e540e26a1e876ea1b16f9ed7c16bdc030bc","SHP_Info_Update_CMTX":"81388da12dfc87898218dec498a729b148420165","SHP_CMSUpdate":"dec0af08c37b10b89413de4848ae0586b7494433","SHP_InfoUpdate":"6593ccf7da474ab595091bbec6464490699ef47e","ShipmentPartyUpdate_OBLeg":"86c3c31f08b9ed65bd1cd6366ad4ea1617dc2501","SHP_CMSUpdate2":"a19c14ba37c4dbdb24a16fce325213dea155e167","SHP_InfoUpdateForDSH":"2f29545f7f76c433d3edd11e8eccbe8ce1e40d9a","VID_SHPUpdate":"160c7246f40cb0ab1c0acad087343fb8db316391"}\r\nMF_SenderDestName=String:IR4.EVG.SHP.UPDATE\r\nIR2NP_SenderCFName=String:MessageFrameworkXACF\r\nMF_AppNamespace=String:http://com.oocl.ir4/shp\r\nIR2NP_AppNamespace=String:http://com.oocl.ir4/shp\r\nIR2NP_MessageVersion=String:1.0\r\nMF_RootElement=String:SHIPMENT_EVT\r\nIR2NP_SenderDestName=String:IR4.EVG.SHP.UPDATE\r\nJMS_TIBCO_PRESERVE_UNDELIVERED=Boolean:true\r\nXCID=String:C7E8AF62-3130-DB25-5375B7F32A6B569F\r\nMF_MessageVersion=String:1.0\r\nJMSXDeliveryCount=Integer:1\r\nDomainId=String:SHP\r\nIR2NP_MessageTimeToLive=Long:0\r\nIR2NP_MessageType=String:SHIPMENT_EVT\r\nMF_SenderCFName=String:MessageFrameworkXACF\r\nProcessId=String:4e947a85-0c72-47c8-9908-380b8bf87ec7\r\nMF_MessageTimeToLive=Long:0\r\nCARRIER_ID=Byte:1\r\nMessageSentTime=String:20180314021321.000\r\n$TextBody:\r\n<?xml version="1.0" encoding="UTF-8" standalone="yes"?><Shipment_EVT xmlns="http://com.oocl.ir4.schema.shp" xmlns:ns2="http://com.oocl.ir4.shp" xmlns:ns4="http://com.oocl.ir4.schema.acm" xmlns:ns3="http://com.oocl.ir4.schema.its"><ShipmentNumber>123456</ShipmentNumber></Shipment_EVT>\r\n',
  'inQueue': 'IR4.SHP.SHP.UPDATE',
  'outQueue': 'IR4.SHPP.WF.REQ'
}

var testcase ={
    msgOut: {
      dbVerifier: "select DECODE(CHKPT_STATE, 'Pending', 'true', 'false') as result from D_CHECKPOINT where CHKPT_TYPE = 'OB_BKG_CONF' and DOMAIN_OBJECT_KEY = '@shipmentNumber@';",
      mongoDBVerifier: [{table:"processInstances",criteria:{domObjKey:"@shipmentNumber@",chkptStatus:"Pending",chkptType:"OB_BKG_CONF"}},{table:"processInstances",criteria:{chkptType:"OB_SI_RECV",chkptStatus:"Pending",domObjKey:"@shipmentNumber@"}}],
      messageVerifier: "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n<ns0:PCM_ProcessData xmlns:ns0=\"http://com.oocl.schema.common\">\n    <ns0:CheckpointEvent xmlns:ns0=\"http://com.oocl.schema.common\">\n        <ns0:Checkpoint_ID>\n            <ns0:DomainObjectRef>\n                <ns0:Namespace>com.oocl.ir4.pcm</ns0:Namespace>\n                <ns0:DomainObjectType>Shipment</ns0:DomainObjectType>\n                <ns0:DomainObjectKey>@shipmentNumber@</ns0:DomainObjectKey>\n            </ns0:DomainObjectRef>\n            <ns0:CheckpointType>OB_BKG_CONF</ns0:CheckpointType>\n        </ns0:Checkpoint_ID>\n        <ns0:Instance_ID>317957d9-9f20-4d9f-a59a-46a2143eb110</ns0:Instance_ID>\n        <ns0:CheckpointState>Pending</ns0:CheckpointState>\n    </ns0:CheckpointEvent>\n</ns0:PCM_ProcessData>"
    },
    bizKey: [
      {
        bizKeyType: "shipmentNumber",
        bizKeyValue: "4045450070"
      },
      {
        bizKeyType: "CMSReference",
        bizKeyValue: "52DZ3I1"
      }
    ]
}

var relationshipData = {

    _id : '5b189c5c132230089fb0e153',
    testCaseId : 'D',
    testCaseName : 'Pending Booking',
    dependency : [
        {
            testCaseId : "C",
            testCaseName : "First POL Load Load Load Load Load Load Load Load ",
            dependency : [
                {
                    testCaseId : "B",
                    testCaseName : "B",
                    dependency : [
                        {
                            testCaseId : "A",
                            testCaseName : "A",
                            dependency : [

                            ]
                        }
                    ]
                }
            ]
        },
        {
            testCaseId : "F",
            testCaseName : "F",
            dependency : [
                {
                    testCaseId : "A",
                    testCaseName : "A",
                    dependency : [

                    ]
                }
            ]
        }
    ]
};
describe('one replacement in URL', function () {
  it('should replace @shipmentNumber@ with 123456', function () {
    var testFun = testcases.__get__('replaceBizKeysWithValue')
    testFun(testcaseData, function (result) {
      expect(JSON.stringify(result[0])).to.be.equal(JSON.stringify(resultData))
    })
  })
})

describe('replaceBizKeytoValueForMsgOut',function () {
  it('replaceBizKeytoValueForMsgOut',function () {
    var replaceBizKeyToValueForMsgOut = testcases.__get__('replaceBizKeyToValueForMsgOut');
    replaceBizKeyToValueForMsgOut(testcase);
    expect(JSON.stringify(testcase)).to.be.contain('4045450070');
  })
})

describe('test testcase module convert to testcase VO ',function () {
    it('should return expected vo data when given a valid testCases data and relationship data',function () {
        //given
        var expectedTestCaseId = "161b8f18-eb6a-4fbf-905b-d5e366dc03ca";
        var relationshipId = "5b189c5c132230089fb0e153";
       //when
        var convertToVo =  testcases.__get__('convertToVo');
        var actualVoData = convertToVo(testcaseData[0], relationshipData);
        //then
        expect(actualVoData.testCaseId).to.be.equal(expectedTestCaseId);
        expect(actualVoData.relationship._id).to.be.equal(relationshipId);
    })

    it('should return expected vo data with out relationship when given a valid testCases data and null relationship data',function () {
        //given
        var expectedTestCaseId = "161b8f18-eb6a-4fbf-905b-d5e366dc03ca";
        //when
        var convertToVo =  testcases.__get__('convertToVo');
        var actualVoData = convertToVo(testcaseData[0], null);
        //then
        expect(actualVoData.testCaseId).to.be.equal(expectedTestCaseId);
        expect(actualVoData.relationship).to.be.equal(null);
    })
    it('should return expected vo data with out relationship when given a valid testCases data and empty relationship data',function () {
        //given
        var expectedTestCaseId = "161b8f18-eb6a-4fbf-905b-d5e366dc03ca";
        //when
        var convertToVo =  testcases.__get__('convertToVo');
        var actualVoData = convertToVo(testcaseData[0], {});
        //then
        expect(actualVoData.testCaseId).to.be.equal(expectedTestCaseId);
        expect(JSON.stringify(actualVoData.relationship)).to.be.equal(JSON.stringify({}));
    })

})